#include "setWifiPage.h"
#include "ui_setWifiPage.h"
#include <QDebug>

/*******************************************************************************
* Function Name :   SetWifipage
* Description   :   构造函数
* Input         :   None
* Output        :   初始化
* Return        :   None
*******************************************************************************/
SetWifiPage::SetWifiPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWifiPage)
{
    ui->setupUi(this);
    connect(ui->button_back,SIGNAL(clicked()),this,SLOT(signal_backDefaultPage()));

    m_wifiInfoForm = new WifiInfoForm();
    ui->stackedWidget_2->addWidget(m_wifiInfoForm);


    m_wifiController = new WifiController();
    connect(m_wifiController,SIGNAL(refreshWifiInfo(QStringList)),
            this,SLOT(on_wifiInfoRefresh(QStringList)));
    m_wifiController->get_WifiInfo();

    m_loadingForm = new LoadingForm();
    ui->stackedWidget_2->addWidget(m_loadingForm);
    ui->stackedWidget_2->setCurrentWidget(m_loadingForm);

    m_keyBoard = new WifiKeyBoard();
    ui->stackedWidget_2->addWidget(m_keyBoard);


    m_curESSID = "";
    isConnected = false;

    connect(m_keyBoard,SIGNAL(has_input_password(QString)),
            this,SLOT(on_password_inputed(QString)));

    connect(m_wifiInfoForm->listView,SIGNAL(clicked(QModelIndex)),
            this,SLOT(on_request_connect_wifi(QModelIndex)));
}

/*******************************************************************************
* Function Name :   ~SetWifiPage
* Description   :   析构函数
* Input         :   None
* Output        :   回收对象
* Return        :   None
*******************************************************************************/
SetWifiPage::~SetWifiPage(void)
{
    delete m_wifiInfoForm;
    delete m_wifiController;
    delete ui;
}

/*******************************************************************************
* Function Name :   on_button_back_clicked
* Description   :   触发返回信号
* Input         :   None
* Output        :   触发signal_backDefaultPage信号
* Return        :   None
*******************************************************************************/
void SetWifiPage::on_button_back_clicked(void)
{
    emit signal_backDefaultPage();
}

void SetWifiPage::on_wifiInfoRefresh(QStringList wifiInfo)
{
    m_wifiInfoForm->set_wifiInfoStringList(wifiInfo);

    if(ui->stackedWidget_2->currentWidget()==m_loadingForm)
    {
        ui->stackedWidget_2->setCurrentWidget(m_wifiInfoForm);
    }
}

void SetWifiPage::on_request_connect_wifi( const QModelIndex & index)
{
    QString ESSID = index.data().toString().split("|").value(0);
    //已有密码，且未连接
    if(m_wifiController->has_Password(ESSID)&&(m_curESSID!=ESSID))
    {
        //连接wifi
        m_wifiController->connect_Wifi(ESSID);
        //显示结果
    }
    //没有密码，且未连接
    else if((!m_wifiController->has_Password(ESSID))&&m_curESSID==ESSID)
    {
        //调出键盘输入密码
        ui->stackedWidget_2->setCurrentWidget(m_keyBoard);
    }

    m_curESSID = ESSID;
}

void SetWifiPage::on_password_inputed(QString psw)
{
    ui->stackedWidget_2->setCurrentWidget(m_wifiInfoForm);
    m_wifiController->connect_Wifi(m_curESSID,psw);
    qDebug()<<"on_password_inputed:"<<m_curESSID<<psw;
}
